<#import "../tpl/pageTep.ftl" as page>
<@page.pageBase currentMenu="礼券管理">
<style>
    .state {
        cursor: pointer;
    }
</style>
    <!--suppress ALL -->
<form id="searchForm" class="form-panel" action="${basepath}/rest/manage/voucher/selectVoucherList">
    <ul class="panel-content">
        <li>
            <div>
                <div class="control-group span5">
                    <div class="search-controls  controls">
                        <input type="text" name="name" id="name" value="" placeholder="搜索礼卷名称、礼卷类型">
                    </div>
                </div>
                <div class="form-actions span2">
                    <button type="submit" class="button  button-primary">搜索</button>
                </div>
            </div>
            <div class="form-actions">
                <a href="${basepath}/rest/manage/voucher/toAdd" class="button button-success">
                    添加
                </a>
                <a class="button button-danger" onclick="return deletesFunction();">
                    批量删除
                </a>
                <a href="javascript: addVoucher();" class="button button-success">
                    <i class="icon-plus-sign icon-white"></i> 派送礼券
                </a>
            </div>
        </li>
    </ul>
</form>
    <div id="grid"></div>
<#--派送礼券-->
<div id="editContent" style="display:none;">
    <form id="editForm" class="form-horizontal">
        <div class="row">
            <div class="control-group span16">
                <div class="controls">
                    <label class="control-label"><s>*</s>选择礼券：</label>
                    <div id="select" style="float: left;">
                        <input type="hidden" class="input-normal " name="voucherID" value="" id="p-value"
                               data-rules="{required : true}">
                    </div>
                </div>
            </div>
            <div class="control-group span16">
                <div class="controls">
                    <label class="control-label"><s>*</s>派送用户：</label>
                    <div id="objselect" style="float: left;">
                        <input type="hidden" class="input-normal obj" name="storeID" value="" id="objvalue"
                               data-rules="{required : true}">
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
<script>
    var Select = BUI.Select;
    var Grid = BUI.Grid,
            Store = BUI.Data.Store,
            columns = [
                {title: '序号', dataIndex: 'id', elCls: 'center', width: '20%'},
                {
                    title: '礼卷类型', dataIndex: 'type', elCls: 'center', width: '20%',
                    renderer: function (value) {
                        switch (value) {
                            case 'cash':
                                return '代金券';
                                break;
                            case 'discount':
                                return '优惠券';
                                break;
                        }
                    }
                },
                {title: '礼卷名称', dataIndex: 'name', elCls: 'center', width: '30%'},
                {
                    title: '指派记录',
                    dataIndex: 'sendAmount',
                    elCls: 'center',
                    width: '20%',
                    renderer: function (value, data) {
                        value = value == '0' ? '无' : '查看';
                        return '<a  href="/rest/manage/voucher/toVoucherSend?id=' + data.id + '">' + value + '</a>';
                    }
                },
                {
                    title: '礼券状态',
                    dataIndex: 'status',
                    elCls: 'center',
                    width: '20%',
                    renderer: function (value, data) {
                        var state = value == 'y' ? '启用' : '禁用';
                        return '<a class="state" data-state="' + value + '" data-id="' + data.id + '">' + state + '</a>';
                    }
                },
                {
                    title: '操作', dataIndex: 'id', elCls: 'center', width: '20%',
                    renderer: function (value, obj, index) {
                        return '<a  href="${basepath}/rest/manage/voucher/toEdit?id=' + value + '">编辑</a>' +
                                '<a style="margin-left:10px;"  href="javascript: deleteFunction(' + value + ');">删除</a>';
                    }
                }
            ];

    var store = new Store({
                url: '/rest/manage/voucher/selectVoucherList',
                autoLoad: true, //自动加载数据
                params: { //配置初始请求的参数
                    length: '10',
                    status: $("#status").val()
                },
                pageSize: 10,	// 配置分页数目
                root: 'data',
                totalProperty: 'total'
            }),
            grid = new Grid.Grid({
                render: '#grid',
                columns: columns,
                loadMask: true, //加载数据时显示屏蔽层
                store: store,
                plugins: [Grid.Plugins.CheckSelection], // 插件形式引入多选表格
                // 底部工具栏
                bbar: {
                    pagingBar: true
                }
            });

    grid.render();

    //修改状态
    function changeStatus(status, id) {
        $.ajax({
            url: '/rest/manage/voucher/updateJson',
            type: 'POST',
            dataType: 'json',
            data: {
                status: status,
                id: id
            },
            success: function () {
                var obj = new Object();
                obj.start = 0;
                store.load(obj);
            }
        })
    }

    $(document).on('click', '.state', function () {
        var state = $(this).attr('data-state');
        var id = $(this).attr('data-id');
        state = state == 'y' ? 'n' : 'y';
        var change = new BUI.Overlay.Dialog({
            title: '提示',
            width:300,height:150,
            bodyContent:'<p>是否确认修改</p>',
            success: function () {
                changeStatus(state, id)
                this.close();
            }
        });
        change.show()

    });

    //单个删除操作
    function deleteFunction(value) {
        BUI.Message.Confirm('确认要删除吗？', function () {
            $.ajax({
                type: "POST",
                url: "${basepath}/rest/manage/voucher/deleteJson",
                dataType: "json",
                data: {
                    id: value
                },
                success: function (data) {
                    var obj = new Object();
                    obj.start = 0; //返回第一页
                    store.load(obj);
                }
            });
        }, 'question');
    }


    //删除选中的记录--批量删除
    function deletesFunction() {
        BUI.Message.Confirm('确认要删除吗？', function () {
            var selections = grid.getSelection();
            var ids = new Array();
            for (var i = 0; i < selections.length; i++) {
                ids[i] = selections[i].id.toString()
            }
            $.ajax({
                type: "POST",
                url: "${basepath}/rest/manage/voucher/deletesJson",
                dataType: "json",
                data: {
                    ids: ids
                },
                success: function (data) {
                    var obj = new Object();
                    obj.start = 0; //返回第一页
                    store.load(obj);
                }
            });
        }, 'question');
    }

    //模糊查询
    var form = new BUI.Form.HForm({
        srcNode: '#searchForm'
    }).render();

    form.on('beforesubmit', function (ev) {
        //序列化成对象
        var obj = form.serializeToObject();
        obj.start = 0; //返回第一页
        store.load(obj);
        return false;
    });
    var editDialog = new BUI.Overlay.Dialog({
        width: 400,
        contentId: 'editContent',
        success: function () {
            var roleID = $('.obj').attr('value');
            var voucherID = $('#p-value').val();
            $.ajax({
                type: "POST",
                url: "/rest/manage/vouchersend/sendVoucher",
                dataType: "json",
                data: {
                    voucherID: voucherID,
                    roleID: roleID
                },
                success: function (data) {
                    var obj = new Object();
                    obj.start = 0; //返回第一页
                    store.load(obj);
                    editDialog.close();
                }
            });
        }
    });

    function addVoucher() {
        var selections = grid.getSelection();
        var ids = new Array();
        for (var i = 0; i < selections.length; i++) {
            ids[i] = selections[i].id.toString()
        }
        editDialog.show();
    }

    //查询礼券
    var pStore = new Store({
        url: '/rest/manage/voucher/selectAllList',
        autoLoad: true,
        root: 'data'
    });
    var sSelect;
    sSelect = new Select.Select({
        render: '#select',
        valueField: '#p-value',
        list: {
            itemTpl: '<li>{name}</li>',
            idField: 'id'
        },
        store: pStore
    }).render();
    //查询用户
    var oStore = new Store({
        url: '/rest/manage/role/selectAllList',
        autoLoad: true,
        root: 'data'
    });
    var oSelect;
    oSelect = new Select.Select({
        render: '#objselect',
        valueField: '#objvalue',
        list: {
            itemTpl: '<li >{role_name}</li>',
            idField: 'id'
        },
        store: oStore
    }).render();
</script>

</@page.pageBase>
